var n;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.YPNetStorage = undefined;
(function (e) {
  e[e.Defalut = 0] = "Defalut";
  e[e.OpenId = 1] = "OpenId";
  e[e.Unionid = 2] = "Unionid";
})(n || (n = {}));
var YPNetStorage = function () {
  function _creator() {}
  _creator.init = function () {
    this.platformAppId = YPSDK.Common.curChannelData.appId;
    this.localStorageCache = null;
    var e = cc.sys.localStorage.getItem(this.localStorageKey + "-Cache");
    e && (this.localStorageCache = JSON.parse(e));
    var t = cc.sys.localStorage.getItem(this.localStorageKey + "-userKey");
    t && (this.userKey = t);
  };
  _creator.initUseKey = function (e) {
    this.userKey = e;
    cc.sys.localStorage.setItem(this.localStorageKey + "-userKey", e);
  };
  _creator.initServer = function (e, t, o, n) {
    this.appId = e;
    this.platformUrl = t;
    this.token = n.tokenData;
    this.loginData = n;
    this.localStorageCache = {
      userUid: this.loginData.userUid,
      appId: this.loginData.appId,
      userKey: o
    };
    if (this.userKey && this.userKey != o) {
      console.error("注意与本地userKey冲突,请检查！！！");
      console.error("注意与本地userKey冲突,请检查！！！");
      console.error("注意与本地userKey冲突,请检查！！！");
    }
    this.userKey = o;
    cc.sys.localStorage.setItem(this.localStorageKey + "-Cache", JSON.stringify(this.localStorageCache));
    cc.sys.localStorage.setItem(this.localStorageKey + "-userKey", o);
  };
  _creator.isHaveLocalCache = function () {
    if (YPSDK.Common.curChannelData.platformType == YPSDK.GamePlatformType.WX || YPSDK.Common.curChannelData.platformType == YPSDK.GamePlatformType.TT) {
      var e = cc.sys.localStorage.getItem(this.localStorageKey + "-userKey");
      if (e) {
        return e;
      }
    }
    var t = cc.sys.localStorage.getItem(this.localStorageKey + "-Cache");
    if (t) {
      try {
        return JSON.parse(t);
      } catch (e) {
        console.error("解析本地缓存失败", e);
        return false;
      }
    }
    return false;
  };
  _creator.getStorageData = function (t) {
    return __awaiter(this, undefined, undefined, function () {
      var o;
      return __generator(this, function (n) {
        switch (n.label) {
          case 0:
            o = "default";
            switch (YPSDK.Common.curChannelData.platformType) {
              case YPSDK.GamePlatformType.WX:
              case YPSDK.GamePlatformType.TT:
                o = this.platformAppId;
                break;
              default:
                if (!this.localStorageCache) {
                  console.warn("无本地存档缓存-h5 localStorageCache");
                  console.log("%c 无本地存档缓存-h5 localStorageCache", "color: red;");
                  return [2, null];
                }
                this.userKey || (this.userKey = "default");
                o = "default/" + this.localStorageCache.appId + "/" + this.localStorageCache.userUid;
            }
            if (this.userKey) {
              console.log("%c 存储路径 " + o, "color: green;");
              return [4, _creator.getStorage(o, t)];
            } else {
              console.warn("无本地存档缓存-userKey");
              console.log("%c 无本地存档缓存-userKey", "color: red;");
              return [2];
            }
          case 1:
            return [2, n.sent()];
        }
      });
    });
  };
  _creator.loadRemoteJson = function (e, t) {
    return new Promise(function (o) {
      cc.assetManager.loadRemote(e, function (e, n) {
        if (e) {
          console.error("读取远程配置错误", e);
          o(false);
        } else {
          t && t(n);
          o(n);
        }
      });
    });
  };
  _creator.sendPost = function (e, t, o, n) {
    var i = new XMLHttpRequest();
    i.open("POST", e);
    i.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
    i.onerror = function () {
      n && n(i.readyState + "|status:" + i.status + "|statusText" + i.statusText);
    };
    i.onreadystatechange = function () {
      4 == i.readyState && 200 == i.status && o && o(i.responseText);
    };
    i.send(JSON.stringify(t));
  };
  _creator.getStorage = function (e, t) {
    return __awaiter(this, undefined, Promise, function () {
      var o;
      return __generator(this, function (n) {
        switch (n.label) {
          case 0:
            o = this.BaseUrl + "/" + e + "/" + this.userKey + "-" + t + ".json?timestamp=" + new Date().getTime();
            return [4, this.httpGet(o)];
          case 1:
            return [2, n.sent()];
        }
      });
    });
  };
  _creator.getStoragePermission = function (e) {
    var t = this;
    return new Promise(function (o) {
      t.StorageServerUrl;
      t.platformUrl;
      t.appId;
      t.userKey;
      t.platformAppId;
      t.token;
      var n = t.StorageServerUrl + "?storageKey=" + e + "&platformUrl=" + encodeURI(t.platformUrl) + "&appId=" + t.appId + "&userKey=" + t.userKey + "&platformAppId=" + t.platformAppId + "&token=" + t.token;
      t.sendPost(n, null, function (e) {
        try {
          var t = JSON.parse(e);
          var n = JSON.parse(t.msg);
          console.log("服务器请求数据", n);
          o({
            authorization: n.sign,
            cosKey: n.cosKey,
            cosHost: n.cosHost
          });
        } catch (t) {
          console.error("返回消息失败", e);
          console.error(t);
          o(null);
        }
      }, function (e) {
        console.error(e);
        o(null);
      });
    });
  };
  _creator.httpGet = function (e) {
    return __awaiter(this, undefined, Promise, function () {
      return __generator(this, function () {
        return [2, new Promise(function (t) {
          var o = new XMLHttpRequest();
          o.open("GET", e, true);
          o.onreadystatechange = function () {
            if (4 == o.readyState) {
              if (200 == o.status) {
                t(o.responseText);
              } else {
                t(null);
              }
            }
          };
          o.onerror = function () {
            t(null);
          };
          o.send();
        })];
      });
    });
  };
  _creator.setStorage = function (e, t) {
    return __awaiter(this, undefined, undefined, function () {
      var o;
      return __generator(this, function (n) {
        switch (n.label) {
          case 0:
            return [4, this.getStoragePermission(e)];
          case 1:
            o = n.sent();
            console.log("==============setStorage===============", o);
            if (o) {
              return [2, new Promise(function (e) {
                var n;
                var i = "https://" + o.cosHost + "/" + (n = o.cosKey, encodeURIComponent(n).replace(/!/g, "%21").replace(/'/g, "%27").replace(/\(/g, "%28").replace(/\)/g, "%29").replace(/\*/g, "%2A")).replace(/%2F/g, "/");
                console.log("保存存档");
                var a = new XMLHttpRequest();
                a.open("PUT", i, true);
                a.setRequestHeader("Authorization", o.authorization);
                a.onreadystatechange = function () {
                  if (4 == a.readyState) {
                    if (200 == a.status) {
                      e(true);
                    } else {
                      e(false);
                    }
                  }
                };
                a.onerror = function () {
                  console.error("文件 " + o.cosKey + " 上传失败，请检查是否没配置 CORS 跨域规则");
                  e(false);
                };
                a.send(t);
              })];
            } else {
              return [2, false];
            }
        }
      });
    });
  };
  _creator.BaseUrl = "https://yp-storage-1318351391.cos.ap-shanghai.myqcloud.com";
  _creator.StorageServerUrl = "https://yp-storage-server.hanyougame.com/gameStorage/getStorageUrl";
  _creator.platformUrl = "";
  _creator.appId = 0;
  _creator.platformAppId = "";
  _creator.token = "";
  _creator.userKey = null;
  _creator.localStorageKey = "YPNetStorage_1.0.0";
  _creator.loginData = null;
  _creator.userKeyType = n.Defalut;
  _creator.localStorageCache = {
    userUid: null,
    appId: null,
    userKey: null
  };
  return _creator;
}();
exports.YPNetStorage = YPNetStorage;